﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbFeedbookDal
	{
		public DbFeedbookDal()
		{
		}
 		#region Insert
 		public static int Insert(DbFeedbook dbFeedbook)
		{	
			string sql = @"Insert Into DbFeedbook (
					FTitle,
					FUser,
					FTel,
					FEmail,
					FQQ,
					FNotes,
					FAddTime,
					FIsReply
				) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}',{7})";
			sql = string.Format(sql,
					SqlHelper.String(dbFeedbook.FTitle),
					SqlHelper.String(dbFeedbook.FUser),
					SqlHelper.String(dbFeedbook.FTel),
					SqlHelper.String(dbFeedbook.FEmail),
					SqlHelper.String(dbFeedbook.FQQ),
					dbFeedbook.FNotes,
					dbFeedbook.FAddTime,
					dbFeedbook.FIsReply
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbFeedbook dbFeedbook)
		{	
			string sql = @"Update DbFeedbook set 

					FTitle= '{0}',
					FUser= '{1}',
					FTel= '{2}',
					FEmail= '{3}',
					FQQ= '{4}',
					FNotes= '{5}',
					FAddTime= '{6}',
					FIsReply= {7}
				where

					FId= {8}";
			sql = string.Format(sql,
					SqlHelper.String(dbFeedbook.FTitle),
					SqlHelper.String(dbFeedbook.FUser),
					SqlHelper.String(dbFeedbook.FTel),
					SqlHelper.String(dbFeedbook.FEmail),
					SqlHelper.String(dbFeedbook.FQQ),
					dbFeedbook.FNotes,
					dbFeedbook.FAddTime,
					dbFeedbook.FIsReply,
					dbFeedbook.FId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 fId)
		{
		
			string sql = @"Delete from DbFeedbook where 
					FId= {0}";
			sql = string.Format(sql,
					fId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbFeedbook a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbFeedbook Select(Int32 fId)
		{
			DbFeedbook dbFeedbook = null;
			string sql = @"select a.* from DbFeedbook a  where
					a.FId= {0}
";
			sql = string.Format(sql,
					fId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbFeedbook = new DbFeedbook(ds.Tables[0].Rows[0]);					
			return dbFeedbook;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbFeedbook a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbFeedbook a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbFeedbook a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
